//
//  BeiDanCiRequest.m
//  Learn
//
//  Created by apple on 2024/4/8.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "BeiDanCiRequest.h"

@implementation BeiDanCiRequest
+(void)request_getReciteDataResult:(void (^)(ReciteModel * _Nonnull, BOOL isDone))reslutBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_CiHuiHomePage];
    [BaseRequest baseGET:url cache:NO autoHideLoading:NO parms:param request:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            ReciteModel *model = [ReciteModel mj_objectWithKeyValues:resultObject[@"data"]];
            reslutBlock(model,YES);
        } else {
            reslutBlock([ReciteModel new],NO);
            [SVProgressHUD showErrorWithStatus:@"请求数据失败"];

        }
    }];
}
#pragma mark   词汇列表
+(void)request_getCihuiListWithVersionName:(NSString *)versionName result:(void (^)(NSMutableArray * _Nonnull, NSMutableArray * _Nonnull, NSMutableArray * _Nonnull))resultBlock{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:versionName forKey:@"versionName"];

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_CiHuiList];
    [BaseRequest baseGET:url cache:NO autoHideLoading:NO parms:param request:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSArray *chuArr = resultObject[@"data"][@"初中"];
            NSArray *xiaoArr = resultObject[@"data"][@"小学"];
            NSMutableArray *chuList = [CiHuiModel mj_objectArrayWithKeyValuesArray:chuArr];
            NSMutableArray *xiaoList = [CiHuiModel mj_objectArrayWithKeyValuesArray:xiaoArr];

            NSMutableArray *allList = [[NSMutableArray alloc]initWithArray:chuList];
            [allList addObjectsFromArray:xiaoList];
            resultBlock(allList,chuList,xiaoList);
        } else {
            resultBlock(@[].mutableCopy,@[].mutableCopy,@[].mutableCopy);
        }
    }];
}

#pragma mark   教程分类
+(void)request_getJiaoCaiListResult:(void(^)(NSMutableArray *chuList,NSMutableArray *xiaoList))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_JiaoCaiList];
    [BaseRequest baseGET:url cache:NO autoHideLoading:YES parms:param request:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSArray *periodList = resultObject[@"data"][@"periodList"];
            NSMutableArray *chuArray = [NSMutableArray array];
            NSMutableArray *xiaoArr = [NSMutableArray array];

            if (periodList.count > 0) {
                NSDictionary *chuDic = periodList.firstObject;
                if ([chuDic[@"periodName"] isEqualToString:@"初中"]) {
                    [chuArray addObjectsFromArray:chuDic[@"child"]];
                }
            }
            if (periodList.count > 1) {
                NSDictionary *chuDic = periodList[1];
                if ([chuDic[@"periodName"] isEqualToString:@"小学"]) {
                    [xiaoArr addObjectsFromArray:chuDic[@"child"]];
                }
            }

            resultBlock(chuArray,xiaoArr);
        } else {
            resultBlock(@[].mutableCopy,@[].mutableCopy);
        }
    }];
}
+(void)request_getRecitingWithJiaoCaiid:(NSString*)versionId ListResult:(void(^)(NSMutableArray *dayList,NSMutableArray *numlist))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:versionId forKey:@"version_id"];

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_RecitingSetting];
    [BaseRequest baseGET:url cache:NO autoHideLoading:YES parms:param request:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSArray *daysList = resultObject[@"data"][@"days"];
            NSArray *countList = resultObject[@"data"][@"count"];
            NSMutableArray *list1 = @[].mutableCopy;
            NSMutableArray *list2 = @[].mutableCopy;
            for (NSNumber *num in daysList) {
                [list1 addObject:[NSString stringWithFormat:@"%@",num]];
            }
            for (NSNumber *num in countList) {
                [list2 addObject:[NSString stringWithFormat:@"%@",num]];
            }
            resultBlock(list1,list2);
        } else {
            resultBlock(@[].mutableCopy,@[].mutableCopy);
        }
    }];
}


+(void)request_postSetPlanWithVersion_Id:(NSString *)versionID days:(NSString *)days count:(NSString *)count reslut:(nonnull void (^)(BOOL isRequest))resultBlock{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:versionID forKey:@"version_id"];
    [param setValue:count forKey:@"count"];
    [param setValue:days forKey:@"end_day"];
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_SetPlan];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            resultBlock(YES);
        } else {
            resultBlock(NO);
        }
    }];
}


+(void)request_getWordListType:(NSInteger )type page:(NSInteger)page  reslut:(void(^)(NSMutableArray *array,BOOL isMore,BOOL isRequest))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:@(page) forKey:@"page"];
    [param setValue:@(10) forKey:@"limit"];
    [param setValue:@(type) forKey:@"type"];

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_wordList];
    [BaseRequest baseGET:url cache:NO autoHideLoading:YES parms:param request:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = (NSDictionary *)resultObject[@"data"];
            NSMutableArray *array = [NSMutableArray array];
            for (NSString *key in dic.allKeys) {
                [array addObject:@{@"key":key,@"words":dic[key]}];
            }
            BOOL ismore = !(array.count <= 10);
            resultBlock(array,ismore,YES);
        } else {
            resultBlock(@[].mutableCopy,NO,NO);
        }
    }];
}



+(void)request_getRecitingWordListWithVersionId:(NSString*)versionId ListResult:(void(^)(NSMutableArray *array))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:versionId forKey:@"versionId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_getRecitingWordList];
    [BaseRequest baseGET:url cache:NO autoHideLoading:YES parms:param request:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSMutableArray *list1 = [WordModel mj_objectArrayWithKeyValuesArray:resultObject[@"data"]];
            resultBlock(list1);
        } else {
            resultBlock(@[].mutableCopy);
        }
    }];
}
+(void)request_getWordInfoWithId:(NSString*)wordId reslut:(void(^)(WordModel *model,BOOL isRequest))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:wordId forKey:@"words_id"];

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_getWordsInfo];
    [BaseRequest baseGET:url cache:NO autoHideLoading:YES parms:param request:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = (NSDictionary *)resultObject[@"data"];
            WordModel *model = [WordModel mj_objectWithKeyValues:dic];
            resultBlock(model,YES);
        } else {
            resultBlock([WordModel new],NO);
        }
    }];
}

@end
